# Maintainer: Zach DeCook <zachdecook@librem.one>
pkgname=verovio
pkgver=3.10.0
pkgrel=0
_commit=845ab54
pkgdesc="Music notation engraving for MEI with MusicXML and Humdrum support"
url="https://www.verovio.org/index.xhtml"
arch="all"
license="LGPL-3.0-only"
depends_dev="$pkgname-libs"
makedepends="
	cmake
	py3-setuptools
	python3-dev
	samurai
	swig
	"
subpackages="
	$pkgname-data::noarch
	$pkgname-libs
	$pkgname-dev
	py3-$pkgname:py3
	"
source="
	$pkgname-$pkgver.tar.gz::https://github.com/rism-digital/verovio/archive/refs/tags/version-$pkgver.tar.gz
	0001-data-change-directory-to-usr-share-verovio.patch
	fix-c-wrapper.patch
	install-prefix.patch
	commit-version.patch
	python-data-path.patch
	"
builddir="$srcdir/$pkgname-version-$pkgver"

[ "$CARCH" = "riscv64" ] && options="$options textrels"

prepare() {
	default_prepare

	sed -i "s|@@COMMIT@@|$_commit|" tools/get_git_commit.sh
}

build() {
	cmake -B build-tools -G Ninja \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-S cmake

	cmake -B build-c-bindings -G Ninja \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-DBUILD_AS_LIBRARY=ON \
		-S cmake

	cmake --build build-tools
	cmake --build build-c-bindings
	python3 setup.py build_ext
}

check() {
	(
		cd build-tools
		./verovio -r ../data ../doc/importer.mei
	)

	(
		cp bindings/c/main.c build-c-bindings/main.c
		# include is only one level up now
		sed -i 's|/../|/|' build-c-bindings/main.c
		cd build-c-bindings
		gcc main.c -o main -L./ $CFLAGS -lverovio
		LD_LIBRARY_PATH=./ ./main
	)
}

package() {
	depends="$pkgname-data"
	DESTDIR="$pkgdir" cmake --install build-tools
	DESTDIR="$pkgdir" cmake --install build-c-bindings
	python3 setup.py install --root="$pkgdir"

	install -Dm644 tools/c_wrapper.h -t "$pkgdir"/usr/include/verovio
	install -Dm644 fonts/VerovioText-1.0.ttf -t "$pkgdir"/usr/share/fonts/verovio/
}

data() {
	pkgdesc="$pkgdesc (data)"
	depends="font-times"

	amove usr/share/verovio
	amove usr/share/fonts
}

libs() {
	default_libs
	depends="$pkgname-data"

	# no versions for now
	amove usr/lib/libverovio.so
}

py3() {
	pkgdesc="$pkgdesc (python module)"
	depends="$pkgname-data"

	amove usr/lib/python3.*
	# data is already in -data
	rm -rf "$subpkgdir"/usr/lib/python3.*/site-packages/verovio/data
	# this should not be installed
	rm -f "$subpkgdir"/usr/lib/python3.*/site-packages/verovio/setup.py
	rm -f "$subpkgdir"/usr/lib/python3.*/site-packages/verovio/__pycache__/setup.cpython-*.pyc
}

sha512sums="
eb691e7c0fb87b954c0bae716a7ab8968db765c374b81fe321d9cea18676c9d335f2e70586cd82b32f92b90c3cd61482ab8cdf27c217cc8579a56db6be9fc051  verovio-3.10.0.tar.gz
89f0d0f797910462ceb1043dee210e74fc00a6c309ef0637173a018224967def5eb1f66c30bfd7499040ccc2f67015d0ec2b37e9e8edc2a5c26c4865ba8c07df  0001-data-change-directory-to-usr-share-verovio.patch
87dd5763a4cdf61dec179659173a1fb207f55f5428ae429f4a5bbe59a997097c5ebc7ca19389974138b446f9837c9a6114625e0869b224fbe6af13a2dfe63c87  fix-c-wrapper.patch
22cf4be4ddd55231980d8cf4740f1152273800811fd78ecc2c82b3ea228b7406d4186a28780146111242c1dc53bfff0e2e264b624ae5871802242bd5ec86eae7  install-prefix.patch
8438687d6b707c08e53fc87598ad74412b530139008bbbd7c4300f83104d2481ebe00923c1681d762445e9eb3c307b0c087c89ab49e2d60b22d7a31ce313063a  commit-version.patch
f62560a8a28d13d25e78a1b33c20a7e69662002c30767853e24ca6268775b3ff48792034803f39b5a9398a46f6ff9b18fbc09a5bef9d38539eeebeefaeb0cdc3  python-data-path.patch
"
